package com.example.android.notepad;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.android.notepad.NotePad;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Search extends Activity {
    EditText et;
    Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        et = findViewById(R.id.editText1);//输入框
        bt = findViewById(R.id.button1);//查询按钮
        bt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String key = et.getText().toString();
                //查询sms数据库的address和body两列，即信息发送者和信息内容，使用SimpleCursorAdapter必须要有_id列
                Cursor cursor = getContentResolver().query(
                        NotePad.Notes.CONTENT_URI, null,
                        "title like ?", new String[]{"%" + key + "%"}, null);
                Bundle data = new Bundle();
                data.putSerializable("data", coverCursorToList(cursor));
                // 创建一个Intent
                Intent intent = new Intent(Search.this, Result.class);
                intent.putExtras(data);
                // 启动Activity
                startActivity(intent);
            }
        });
    }

    private ArrayList<Map<String, String>> coverCursorToList(Cursor cursor) {
        ArrayList<Map<String, String>> result = new ArrayList<>();
        // 遍历Cursor结果集
        while (cursor.moveToNext()) {
            // 将结果集中的数据存入ArrayList中
            Map<String, String> map = new HashMap<>();
            // 取出查询记录中的值
            map.put("title", cursor.getString(4));
            result.add(map);
        }
        return result;
    }
}
